import java.util.Stack;

/**
 * @User: vitobo
 * @Date: 2024-06-23
 * @Description: 用栈实现队列
 */

public class MyQueue2 {
    private Stack<Integer> s1;
    private Stack<Integer> s2;

    public MyQueue2(){
        s1 = new Stack<>();
        s2 = new Stack<>();
    }

    // 入队
    public void push(int x){
        s1.push(x);
    }

    // 出队
    public int pop(){
        if(empty()){
            return -1;
        }
        if(s2.empty()){
            while (!s1.empty()){
                s2.push(s1.pop());
            }
        }
        return s2.pop();
    }

    // 获取队头元素
    public int peek(){
        if(empty()){
            return -1;
        }
        if(s2.empty()){
            while (!s1.empty()){
                s2.push(s1.pop());
            }
        }
        return s2.pop();
    }

    // 判空, 如果两个栈都是空的, 则队列就是空的
    public boolean empty(){
        return s1.empty() && s2.empty();
    }
}
